//
//  HttpInputRequest.m
//  XTCloud
//
//  Created by QA on 2017/3/27.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "HttpPlatformInputRequest.h"
#import "NSKeyedArchiverUserLogin.h"
#import "AESCrypt.h"

@interface HttpPlatformInputRequest()

@property (nonatomic, strong) NSMutableDictionary *allDictionary;
@property (nonatomic, strong) NSMutableDictionary *dictionary;

@end

@implementation HttpPlatformInputRequest

- (instancetype)init
{
    self = [super init];
    if(self){
        self.dictionary = [NSMutableDictionary dictionary];
        self.allDictionary = [NSMutableDictionary dictionary];
    }
    return self;
}

//设置接口名称
- (void)setInterface:(NSString*)interface
{
    if(!interface) interface = @"";
    [_allDictionary setObject:interface forKey:@"FunType"];
}

//设置链接库ID（站点ID）
- (void)setSiteId:(NSInteger)siteId
{
    [_allDictionary setObject:@(siteId) forKey:@"SiteId"];
}

//设置IntType(0-HOLUIDB)
- (void)setIntType:(NSInteger)intType
{
    [_allDictionary setObject:@(intType) forKey:@"IntType"];
}

//设置用户ID
- (void)setUserId:(NSInteger)userId
{
    [_allDictionary setObject:@(userId) forKey:@"UserId"];
}

//设置数据展示方式，默认JSON-Array类型数据
- (void)setIsOrder:(NSInteger)isOrder
{
    [_allDictionary setObject:@(isOrder) forKey:@"IsOrder"];
}

//设置传递的参数
- (void)setParameterVale:(id)value key:(NSString*)key
{
    if(!key) return;
    if(!value) value = @"";
    [_dictionary setObject:value forKey:key];
}

//获取封装参数,并进行加密
- (NSDictionary*)getParame
{
    //基础数据容错处理
    if (![self.allDictionary objectForKey:@"SiteId"])
        [self setSiteId:0];
    if (![self.allDictionary objectForKey:@"IntType"])
        [self setIntType:0];
    if (![self.allDictionary objectForKey:@"UserId"])
        [self setUserId:0];
    if (![self.allDictionary objectForKey:@"FunType"])
        [self setInterface:@""];
    if (![self.allDictionary objectForKey:@"IsOrder"])
        [self setIsOrder:0];
    
    //参数添加
    if (self.dictionary)
        [self.allDictionary setValue:self.dictionary forKey:@"Parameter"];
    
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self.allDictionary options:NSJSONWritingPrettyPrinted error:&error];
    if ([jsonData length] > 0 && error == nil) {
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSString *jsonEncrypt = [AESCrypt encrypt:jsonString];
        
        NSDictionary *dictionary = [NSDictionary dictionaryWithObject:jsonEncrypt forKey:@"jsonParm"];
        return dictionary;
    }
    return [NSDictionary dictionary];
}

@end
